package com.feijin.testwisdomzonedemo.actions;


import android.util.Base64;

import com.feijin.testwisdomzonedemo.event.InfoEvent;
import com.feijin.testwisdomzonedemo.model.AvatarDto;
import com.feijin.testwisdomzonedemo.net.RMer;
import com.feijin.testwisdomzonedemo.ui.impl.InfoView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lgc.garylianglib.actions.Action;
import com.lgc.garylianglib.model.BaseDto;
import com.lgc.garylianglib.net.CollectionsUtils;
import com.lgc.garylianglib.util.L;
import com.lgc.garylianglib.util.data.StreamUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.FileInputStream;

import retrofit2.Call;


/**
 * <pre>
 *     author : feijin_lgc
 *     e-mail : 595184932@qq.com
 *     time   : 2017/11/24 16:45
 *     desc   :   个人信息
 *     version: 1.0
 * </pre>
 */
public class UserInfoAction extends BaseAction<InfoView> {

    public UserInfoAction(InfoView view) {
        super.attachView(view);
    }

    public UserInfoAction() {

    }

    /**
     * 上传头像
     */
    public void uploadAvatar(String avatar) {
        String imgBase64Code = null;
        try {
            FileInputStream in = new FileInputStream(avatar);
            byte buffer[] = StreamUtil.read(in);
            byte[] encod = Base64.encode(buffer, Base64.DEFAULT);
            imgBase64Code = new String(encod);
        } catch (Exception e) {

        }
        RMer rMer = RMer.instance(false);
        Call<BaseDto> call = rMer.api().uploadAvatar(CollectionsUtils.generateMap("data", imgBase64Code));
        baseDtoCall = post(rMer, call, InfoEvent.ACTION_KEY_SUCCESS_UPLOAD, false);


    }

    /**
     * /**
     * sticky:表明优先接收最高级  threadMode = ThreadMode.MAIN：表明在主线程
     *
     * @param action
     */
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void MessageEvent(Action action) {
        L.e("xx", "action   接收到数据更新.....onStoreChange 1  action : " + action.toString());
        String msg = "";
        switch (action.getType()) {
            //修改头像 成功
            case InfoEvent.ACTION_KEY_SUCCESS_UPLOAD:
                L.e("xx", "接收 修改头像  信息成功....." + action.getUserData().toString());

                AvatarDto avatarDto = new Gson().fromJson(action.getUserData().toString(), new TypeToken<AvatarDto>() {
                }.getType());
                if (avatarDto.getData() != null) {
                    view.uploadAvatar(avatarDto);
                } else {
                    msg = action.getMsg(action);
                    view.onError(msg, action.getErrorType());
                }
                break;
            //   失败
            case InfoEvent.ACTION_KEY_ERROR:
                msg = action.getMsg(action);
                view.onError(msg, action.getErrorType());
                L.e("xx", "接收信息失败....errorType ." + action.getErrorType() + " msg " + msg);
                break;
        }


    }

    public void toRegister() {

        register(this);
    }

    public void toUnregister() {

        unregister(this);
    }
}
